﻿using ElasticSearch.DAL;
using ElasticsearchCRUD;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElasticSearch.BLL.Helper
{
    public class SaveToElasticsearchHelper
    {
        private static CapturedataEntities _db = new CapturedataEntities();
        private static Stopwatch stopwatch = new Stopwatch();
        public static string SaveToElasticsearchPtypecollection()
        {
            var startDate = DateTime.Now;
            IElasticsearchMappingResolver elasticsearchMappingResolver = new ElasticsearchMappingResolver();
            using (var elasticsearchContext = new ElasticsearchContext("http://localhost:9200/", elasticsearchMappingResolver))
            {
                int pointer = 0;
                int interval = 10000;
                int length = _db.ptypecollection.Count();
                while (pointer < length)
                {
                    using (CapturedataEntities _db = new CapturedataEntities())
                    {
                        var collection = _db.ptypecollection.OrderBy(t => t.ID).Skip(pointer).Take(interval);
                        foreach (var item in collection)
                        {
                            elasticsearchContext.AddUpdateDocument(item, item.ID);
                        }
                        elasticsearchContext.SaveChanges();
                        pointer = pointer + interval;
                    }

                }
            }
            var endDate = DateTime.Now;
            return string.Format("开始时间：" + startDate + " 结束时间：" + endDate + "耗时：" + endDate.Subtract(startDate).TotalSeconds +"秒");
        }
    }
}
